﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VAdvantage.Classes;
using VAdvantage.Grids;
using System.Windows.Data;
using System.Windows.Controls;

namespace VAdvantage.Controls
{
    public class VDatePicker : System.Windows.Controls.DatePicker, IControl
    {
        #region "Declaration"

        private String _columnName;
        protected int _displayType = DisplayType.Date;
        private String _title;
        private bool _setting;
        private String _oldText;
        private String _initialText;
        //
        //
        private bool _readOnly;
        private bool _mandatory;
        private GridField _field;

        private bool _focus;

        public event EventHandler VetoableChangeListener;

        private Binding binding = null;      

        #endregion


        public VDatePicker()
        {
            // SetCultureFormat();
        }

        public VDatePicker(int displayType)
        {
            _displayType = displayType;
        }

        /// <summary>
        /// Construtor
        /// </summary>
        /// <param name="columnName"></param>
        /// <param name="mandatory"></param>
        /// <param name="isReadOnly"></param>
        /// <param name="isUpdateable"></param>
        /// <param name="displayType"></param>
        /// <param name="title"></param>
        public VDatePicker(String columnName, bool mandatory, bool isReadOnly, bool isUpdateable,
                                int displayType, String title)
        {
           
            _columnName = columnName;
            _title = title;
            _displayType = displayType;
            _readOnly = isReadOnly;
            _mandatory = mandatory;

            //	ReadWrite
            if (isReadOnly || !isUpdateable)
                SetReadOnly(true);
            else
                SetReadOnly(false);
        }


        #region "Icontrol"
        public bool GetIsMandatory()
        {
            return _mandatory;
        }
        /// <summary>
        /// Sets Mandatory
        /// </summary>
        /// <param name="value">bool type value</param>
        public void SetIsMandatory(bool value)
        {
            _mandatory = value;
        }
        public bool GetIsReadonly()
        {
            return _readOnly;
        }
        public GridField GetField()
        {
            return _field;
        }
        public void SetField(GridField field)
        {
            _field = field;
            //if (_field != null && Model.MRole.GetDefault().IsShowPreference())
            //Framework.ValuePreference.AddPreferenceMenuItem(this.preferenceItem_Click, popupMenu);
        }
        public int GetDisplayType()
        {
            return _displayType;
        }
        public void SetDisplayType(int displayType)
        {
            _displayType = displayType;
        }
        public string GetName()
        {
            return this._columnName;
        }

        public object GetValue()
        {
            if (this.Text == null || this.Text == "")
            {
                if (_field != null)
                    return DBNull.Value;
                else
                    return null;
            }
            //String value = this.Value.ToString();
            //if (value == null || value.Length == 0)
            //    return null;
            //
            DateTime dt;
            if (this.Text != null)
                dt = Convert.ToDateTime(this.Text);
            else
                return null;

            if (_displayType == DisplayType.Date)
            {
                return Convert.ToDateTime(DataBase.GlobalVariable.SetDateFormat(dt, true));
            }
            else
            {
                return Convert.ToDateTime(DataBase.GlobalVariable.SetDateFormat(dt, false));
            }
            //return dt;

        }

        public string GetSelectedText()
        {
            return "";

        }
        /// <summary>
        /// Set value
        /// </summary>
        /// <param name="value">value</param>
        public void SetValue(object value)
        {

            if (value != null && value.ToString() != "")
                this.Text = value.ToString();
            else
                this.Text = null;
            SetColor();
        }

        public string GetText()
        {
            //return this.Value.ToString();// date+time
            //return this.Value.ToString(); //.ToShortDateString(); //date only
            return Convert.ToString(this.Text);
        }

        public void SetReadOnly(bool readOnly)
        {
            if (readOnly && _mandatory && this.Text == null)
            {
                this.IsEnabled = true;
                //this._backColor = DataBase.GlobalVariable.MANDATORY_TEXT_BACK_COLOR;
                _readOnly = false;
                return;
            }

            this.IsEnabled = !readOnly;
            _readOnly = readOnly;

            if (readOnly)
            {
                //this._backColor = DataBase.GlobalVariable.READONLY_TEXT_BACK_COLOR;
            }
            else
            {
                //this._backColor = DataBase.GlobalVariable.NORMAL_TEXT_BACK_COLOR;
                // SetColor();
            }
        }

        public void SetBackGround(bool value)
        {
            //this.BackColor = CommonFunctions.GetBackGroundColor(value);
        }

        private void SetColor()
        {
            //if (_mandatory && !_readOnly)
            //{
            //    if (this.Value == null)
            //    {
            //        this._backColor = DataBase.GlobalVariable.MANDATORY_TEXT_BACK_COLOR;
            //    }
            //    else
            //    {
            //        this._backColor = DataBase.GlobalVariable.NORMAL_TEXT_BACK_COLOR;
            //        if (_field != null)
            //        {
            //            _field.SetError(false);
            //        }
            //    }
            //}
        }

        public void SetName(string name)
        {
            this._columnName = name;
        }

        public bool IsDisabled()
        {
            return !this.IsEnabled;
        }

        public void SetDisabled(bool disabled)
        {
            this.IsEnabled = !disabled;
        }

        public int GetTabIndex()
        {
            return this.TabIndex;
        }

        public void SetTabindex(int tabindex)
        {
            this.TabIndex = tabindex;
        }

        public bool Multiline
        {
            get;set;
        }

        public bool Visible
        {
            get
            {
                return this.Visibility == System.Windows.Visibility.Visible;
            }
            set
            {
                this.Visibility = value?System.Windows.Visibility.Visible:System.Windows.Visibility.Collapsed;
            }
        }

        public bool Focused
        {
            get { return _focus; }
        }


        #endregion

        /// <summary>
        /// event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void preferenceItem_Click(object sender, EventArgs e)
        {
            //if (Model.MRole.GetDefault().IsShowPreference())
            //{
            //    // opens Value preference window
            //    Framework.ValuePreference obj = Framework.ValuePreference.Start(_field, GetValue(), GetText());
            //    if (obj != null)
            //    {
            //        obj.ShowDialog();
            //    }
            //}
            return;
        }


        protected override void OnGotFocus(System.Windows.RoutedEventArgs e)
        {
            _focus = true;
            base.OnGotFocus(e);
            
        }

        protected override void OnLostFocus(System.Windows.RoutedEventArgs e)
        {
            _focus = false;
            base.OnLostFocus(e);
        }

        public void Dispose()
        {
            _field = null;
            binding = null;
        }


        public string BindValue
        {
            set
            {
                binding = new Binding(value);
                binding.Mode = BindingMode.OneWay;
                this.SetBinding(DatePicker.SelectedDateProperty, binding);
            }   
        }
    }
}

